{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7 数组的基本函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.        ,  2.71828183,  7.3890561 , 20.08553692])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.        , 0.84147098, 0.90929743, 0.14112001])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sin(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.add(a, a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`argmax()`函数用来返回某个轴上最大值得位置。如果没有定义轴的方向，则数组自动扩展成一维数组，然后返回最大元素的索引值。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(12).reshape(3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 2, 2])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmax(a, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmax(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**矩阵乘法**：`np.dot`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(12).reshape(3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.arange(12, 24).reshape(4, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[12, 13, 14],\n",
       "       [15, 16, 17],\n",
       "       [18, 19, 20],\n",
       "       [21, 22, 23]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[114, 120, 126],\n",
       "       [378, 400, 422],\n",
       "       [642, 680, 718]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(a, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 8 复制和指代\n",
    "\n",
    "数组的`view()`和切片会返回数组的指代。而`copy`方法会建立一个新的数组对象，和原数组没有任何关系。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 9 线性代数\n",
    "\n",
    "Numpy可用于计算矩阵相乘、分解矩阵、求解线性方程等线性问题。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求解矩阵中的逆矩阵 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy.linalg as LA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(1, 10, 1).reshape(3, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# inva = LA.inv(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以使用solve()函数解决下面这个线性方程组。\n",
    "```\n",
    "x1 + 2x2 = 2\n",
    "2x1 - x2 = 4\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[1, 2], [2, -1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2],\n",
       "       [ 2, -1]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "B = np.array([2, 4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2., 0.])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LA.solve(A, B)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用eig()函数求解一个矩阵的特征值和特征向量。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "q = np.diag((4, 5, 6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 0, 0],\n",
       "       [0, 5, 0],\n",
       "       [0, 0, 6]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y = LA.eig(q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4., 5., 6.])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 10 使用数组来处理数据源 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y = np.mgrid[0:6*np.pi:0.25, 0:0.4*np.pi:0.25]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],\n",
       "       [ 0.25,  0.25,  0.25,  0.25,  0.25,  0.25],\n",
       "       [ 0.5 ,  0.5 ,  0.5 ,  0.5 ,  0.5 ,  0.5 ],\n",
       "       [ 0.75,  0.75,  0.75,  0.75,  0.75,  0.75],\n",
       "       [ 1.  ,  1.  ,  1.  ,  1.  ,  1.  ,  1.  ],\n",
       "       [ 1.25,  1.25,  1.25,  1.25,  1.25,  1.25],\n",
       "       [ 1.5 ,  1.5 ,  1.5 ,  1.5 ,  1.5 ,  1.5 ],\n",
       "       [ 1.75,  1.75,  1.75,  1.75,  1.75,  1.75],\n",
       "       [ 2.  ,  2.  ,  2.  ,  2.  ,  2.  ,  2.  ],\n",
       "       [ 2.25,  2.25,  2.25,  2.25,  2.25,  2.25],\n",
       "       [ 2.5 ,  2.5 ,  2.5 ,  2.5 ,  2.5 ,  2.5 ],\n",
       "       [ 2.75,  2.75,  2.75,  2.75,  2.75,  2.75],\n",
       "       [ 3.  ,  3.  ,  3.  ,  3.  ,  3.  ,  3.  ],\n",
       "       [ 3.25,  3.25,  3.25,  3.25,  3.25,  3.25],\n",
       "       [ 3.5 ,  3.5 ,  3.5 ,  3.5 ,  3.5 ,  3.5 ],\n",
       "       [ 3.75,  3.75,  3.75,  3.75,  3.75,  3.75],\n",
       "       [ 4.  ,  4.  ,  4.  ,  4.  ,  4.  ,  4.  ],\n",
       "       [ 4.25,  4.25,  4.25,  4.25,  4.25,  4.25],\n",
       "       [ 4.5 ,  4.5 ,  4.5 ,  4.5 ,  4.5 ,  4.5 ],\n",
       "       [ 4.75,  4.75,  4.75,  4.75,  4.75,  4.75],\n",
       "       [ 5.  ,  5.  ,  5.  ,  5.  ,  5.  ,  5.  ],\n",
       "       [ 5.25,  5.25,  5.25,  5.25,  5.25,  5.25],\n",
       "       [ 5.5 ,  5.5 ,  5.5 ,  5.5 ,  5.5 ,  5.5 ],\n",
       "       [ 5.75,  5.75,  5.75,  5.75,  5.75,  5.75],\n",
       "       [ 6.  ,  6.  ,  6.  ,  6.  ,  6.  ,  6.  ],\n",
       "       [ 6.25,  6.25,  6.25,  6.25,  6.25,  6.25],\n",
       "       [ 6.5 ,  6.5 ,  6.5 ,  6.5 ,  6.5 ,  6.5 ],\n",
       "       [ 6.75,  6.75,  6.75,  6.75,  6.75,  6.75],\n",
       "       [ 7.  ,  7.  ,  7.  ,  7.  ,  7.  ,  7.  ],\n",
       "       [ 7.25,  7.25,  7.25,  7.25,  7.25,  7.25],\n",
       "       [ 7.5 ,  7.5 ,  7.5 ,  7.5 ,  7.5 ,  7.5 ],\n",
       "       [ 7.75,  7.75,  7.75,  7.75,  7.75,  7.75],\n",
       "       [ 8.  ,  8.  ,  8.  ,  8.  ,  8.  ,  8.  ],\n",
       "       [ 8.25,  8.25,  8.25,  8.25,  8.25,  8.25],\n",
       "       [ 8.5 ,  8.5 ,  8.5 ,  8.5 ,  8.5 ,  8.5 ],\n",
       "       [ 8.75,  8.75,  8.75,  8.75,  8.75,  8.75],\n",
       "       [ 9.  ,  9.  ,  9.  ,  9.  ,  9.  ,  9.  ],\n",
       "       [ 9.25,  9.25,  9.25,  9.25,  9.25,  9.25],\n",
       "       [ 9.5 ,  9.5 ,  9.5 ,  9.5 ,  9.5 ,  9.5 ],\n",
       "       [ 9.75,  9.75,  9.75,  9.75,  9.75,  9.75],\n",
       "       [10.  , 10.  , 10.  , 10.  , 10.  , 10.  ],\n",
       "       [10.25, 10.25, 10.25, 10.25, 10.25, 10.25],\n",
       "       [10.5 , 10.5 , 10.5 , 10.5 , 10.5 , 10.5 ],\n",
       "       [10.75, 10.75, 10.75, 10.75, 10.75, 10.75],\n",
       "       [11.  , 11.  , 11.  , 11.  , 11.  , 11.  ],\n",
       "       [11.25, 11.25, 11.25, 11.25, 11.25, 11.25],\n",
       "       [11.5 , 11.5 , 11.5 , 11.5 , 11.5 , 11.5 ],\n",
       "       [11.75, 11.75, 11.75, 11.75, 11.75, 11.75],\n",
       "       [12.  , 12.  , 12.  , 12.  , 12.  , 12.  ],\n",
       "       [12.25, 12.25, 12.25, 12.25, 12.25, 12.25],\n",
       "       [12.5 , 12.5 , 12.5 , 12.5 , 12.5 , 12.5 ],\n",
       "       [12.75, 12.75, 12.75, 12.75, 12.75, 12.75],\n",
       "       [13.  , 13.  , 13.  , 13.  , 13.  , 13.  ],\n",
       "       [13.25, 13.25, 13.25, 13.25, 13.25, 13.25],\n",
       "       [13.5 , 13.5 , 13.5 , 13.5 , 13.5 , 13.5 ],\n",
       "       [13.75, 13.75, 13.75, 13.75, 13.75, 13.75],\n",
       "       [14.  , 14.  , 14.  , 14.  , 14.  , 14.  ],\n",
       "       [14.25, 14.25, 14.25, 14.25, 14.25, 14.25],\n",
       "       [14.5 , 14.5 , 14.5 , 14.5 , 14.5 , 14.5 ],\n",
       "       [14.75, 14.75, 14.75, 14.75, 14.75, 14.75],\n",
       "       [15.  , 15.  , 15.  , 15.  , 15.  , 15.  ],\n",
       "       [15.25, 15.25, 15.25, 15.25, 15.25, 15.25],\n",
       "       [15.5 , 15.5 , 15.5 , 15.5 , 15.5 , 15.5 ],\n",
       "       [15.75, 15.75, 15.75, 15.75, 15.75, 15.75],\n",
       "       [16.  , 16.  , 16.  , 16.  , 16.  , 16.  ],\n",
       "       [16.25, 16.25, 16.25, 16.25, 16.25, 16.25],\n",
       "       [16.5 , 16.5 , 16.5 , 16.5 , 16.5 , 16.5 ],\n",
       "       [16.75, 16.75, 16.75, 16.75, 16.75, 16.75],\n",
       "       [17.  , 17.  , 17.  , 17.  , 17.  , 17.  ],\n",
       "       [17.25, 17.25, 17.25, 17.25, 17.25, 17.25],\n",
       "       [17.5 , 17.5 , 17.5 , 17.5 , 17.5 , 17.5 ],\n",
       "       [17.75, 17.75, 17.75, 17.75, 17.75, 17.75],\n",
       "       [18.  , 18.  , 18.  , 18.  , 18.  , 18.  ],\n",
       "       [18.25, 18.25, 18.25, 18.25, 18.25, 18.25],\n",
       "       [18.5 , 18.5 , 18.5 , 18.5 , 18.5 , 18.5 ],\n",
       "       [18.75, 18.75, 18.75, 18.75, 18.75, 18.75]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25],\n",
       "       [0.  , 0.25, 0.5 , 0.75, 1.  , 1.25]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "Z = np.sqrt(np.abs(np.cos(x)+np.cos(y)))  # 根据x, y的坐标计算Z值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.41421356, 1.4031794 , 1.37024909, 1.3159365 , 1.24108916,\n",
       "        1.14687504],\n",
       "       [1.4031794 , 1.39205777, 1.35885797, 1.30407105, 1.22850101,\n",
       "        1.13324083],\n",
       "       [1.37024909, 1.35885797, 1.32482645, 1.26857063, 1.19074971,\n",
       "        1.09220187],\n",
       "       [1.3159365 , 1.30407105, 1.26857063, 1.20970151, 1.12782586,\n",
       "        1.02323567],\n",
       "       [1.24108916, 1.22850101, 1.19074971, 1.12782586, 1.03952134,\n",
       "        0.92499982],\n",
       "       [1.14687504, 1.13324083, 1.09220187, 1.02323567, 0.92499982,\n",
       "        0.79413143],\n",
       "       [1.03476432, 1.0196321 , 0.97381711, 0.89578238, 0.78169016,\n",
       "        0.62133692],\n",
       "       [0.90650645, 0.88919422, 0.83626342, 0.74393737, 0.6017111 ,\n",
       "        0.37023818],\n",
       "       [0.76410285, 0.74348207, 0.6792906 , 0.56173128, 0.35235702,\n",
       "        0.3175287 ],\n",
       "       [0.60977568, 0.58372836, 0.49940859, 0.32173785, 0.29643096,\n",
       "        0.55933108],\n",
       "       [0.44593316, 0.40959591, 0.27647594, 0.26354268, 0.51072626,\n",
       "        0.69700879],\n",
       "       [0.27513201, 0.2112109 , 0.21614767, 0.43887756, 0.61967739,\n",
       "        0.78037172],\n",
       "       [0.10003751, 0.14518979, 0.33527591, 0.5082358 , 0.67058944,\n",
       "        0.82138306],\n",
       "       [0.07661804, 0.15879942, 0.34138997, 0.51228977, 0.67366711,\n",
       "        0.82389764],\n",
       "       [0.25207799, 0.18015475, 0.24263991, 0.45251278, 0.62940796,\n",
       "        0.78812076],\n",
       "       [0.42360435, 0.38516628, 0.23879532, 0.29811154, 0.5293931 ,\n",
       "        0.71080025],\n",
       "       [0.5885205 , 0.56148802, 0.47322187, 0.2793658 , 0.33666202,\n",
       "        0.5816539 ],\n",
       "       [0.74425299, 0.72306634, 0.65688284, 0.53441686, 0.30694432,\n",
       "        0.36161461],\n",
       "       [0.88837166, 0.87069893, 0.81657012, 0.72172922, 0.57402657,\n",
       "        0.32330568],\n",
       "       [1.01862758, 1.003252  , 0.95665287, 0.87709237, 0.76020028,\n",
       "        0.5940745 ],\n",
       "       [1.13298817, 1.1191848 , 1.07761067, 1.00764629, 0.9077249 ,\n",
       "        0.77394092],\n",
       "       [1.22966885, 1.21696257, 1.17884182, 1.11524632, 1.02585953,\n",
       "        0.90961961],\n",
       "       [1.30716096, 1.29521512, 1.2594651 , 1.20014943, 1.11757419,\n",
       "        1.01192497],\n",
       "       [1.36425526, 1.35281367, 1.31862617, 1.26209401, 1.18384742,\n",
       "        1.08467266],\n",
       "       [1.40006082, 1.38891422, 1.35563743, 1.30071486, 1.22493779,\n",
       "        1.12937711],\n",
       "       [1.41401889, 1.40298319, 1.37004817, 1.31572728, 1.24086733,\n",
       "        1.14663498],\n",
       "       [1.40591167, 1.39481183, 1.36167918, 1.30701052, 1.23162086,\n",
       "        1.13662218],\n",
       "       [1.37586567, 1.36452144, 1.33063478, 1.27463533, 1.19720869,\n",
       "        1.09924006],\n",
       "       [1.32434975, 1.31256035, 1.2772959 , 1.21884828, 1.13763112,\n",
       "        1.03403318],\n",
       "       [1.25216779, 1.23969214, 1.20229228, 1.14000572, 1.05272336,\n",
       "        0.93981197],\n",
       "       [1.16044617, 1.1469733 , 1.1064438 , 1.0384239 , 0.94177366,\n",
       "        0.81360782],\n",
       "       [1.05061618, 1.03571559, 0.9906447 , 0.91404772, 0.80255633,\n",
       "        0.64739225],\n",
       "       [0.92439167, 0.90742073, 0.85561821, 0.7656297 , 0.62833293,\n",
       "        0.41209505],\n",
       "       [0.78374234, 0.76365207, 0.70130922, 0.58816743, 0.39313403,\n",
       "        0.26537817],\n",
       "       [0.63086298, 0.60572314, 0.52494824, 0.36010688, 0.24841416,\n",
       "        0.53543397],\n",
       "       [0.46813921, 0.43366662, 0.31102553, 0.22171336, 0.49045222,\n",
       "        0.68229269],\n",
       "       [0.29811028, 0.2403792 , 0.18316031, 0.42360523, 0.60895645,\n",
       "        0.77188594],\n",
       "       [0.12342944, 0.12590771, 0.32738756, 0.5030669 , 0.66668048,\n",
       "        0.81819485],\n",
       "       [0.05317747, 0.16810632, 0.34581728, 0.5152507 , 0.67592148,\n",
       "        0.82574197],\n",
       "       [0.22895457, 0.1460569 , 0.26456992, 0.46464065, 0.63818297,\n",
       "        0.79514618],\n",
       "       [0.40115891, 0.36033442, 0.19624228, 0.32769294, 0.54659786,\n",
       "        0.72370517],\n",
       "       [0.5671033 , 0.53899775, 0.44630563, 0.23085714, 0.37160671,\n",
       "        0.60255414],\n",
       "       [0.72419823, 0.70240693, 0.63407069, 0.50611455, 0.25449043,\n",
       "        0.40026812],\n",
       "       [0.86999229, 0.85193838, 0.79653571, 0.69898172, 0.54515033,\n",
       "        0.26871721],\n",
       "       [1.00221041, 0.986579  , 0.93915295, 0.85797119, 0.73805691,\n",
       "        0.5654627 ],\n",
       "       [1.11878937, 1.10480861, 1.0626722 , 0.99165444, 0.8899393 ,\n",
       "        0.753002  ],\n",
       "       [1.21791   , 1.20507974, 1.16657075, 1.10226749, 1.01173468,\n",
       "        0.8936594 ],\n",
       "       [1.29802555, 1.28599484, 1.24998115, 1.19019292, 1.10687516,\n",
       "        1.00009634],\n",
       "       [1.35788584, 1.34639013, 1.31203526, 1.25520629, 1.17650171,\n",
       "        1.07665051],\n",
       "       [1.39655678, 1.38538199, 1.35201827, 1.29694245, 1.22093126,\n",
       "        1.12503031],\n",
       "       [1.41343492, 1.40239463, 1.36944545, 1.31509967, 1.24020183,\n",
       "        1.14591476],\n",
       "       [1.40825688, 1.39717568, 1.36410044, 1.30953286, 1.23429727,\n",
       "        1.13952175],\n",
       "       [1.38110347, 1.36980261, 1.3360499 , 1.28028733, 1.20322445,\n",
       "        1.10578892],\n",
       "       [1.33239839, 1.32068084, 1.28563915, 1.22758883, 1.14699075,\n",
       "        1.04432171],\n",
       "       [1.26290168, 1.25053312, 1.21346744, 1.15178537, 1.06546843,\n",
       "        0.95406657],\n",
       "       [1.17369782, 1.16037881, 1.12033438, 1.05321196, 0.95805474,\n",
       "        0.8323995 ],\n",
       "       [1.06617879, 1.05149876, 1.00713444, 0.93189382, 0.82282411,\n",
       "        0.67235376],\n",
       "       [0.9420224 , 0.92537486, 0.87463636, 0.78682595, 0.65399428,\n",
       "        0.45025389],\n",
       "       [0.80316607, 0.78357396, 0.7229511 , 0.61381154, 0.4305555 ,\n",
       "        0.19900227],\n",
       "       [0.6517766 , 0.62747522, 0.5499048 , 0.39560283, 0.18677517,\n",
       "        0.50976946],\n",
       "       [0.49021637, 0.45741066, 0.3433579 , 0.16732915, 0.46838617,\n",
       "        0.66660749],\n",
       "       [0.32100648, 0.2682491 , 0.13918434, 0.40652918, 0.59720393,\n",
       "        0.76264833],\n",
       "       [0.14678739, 0.09767825, 0.3176018 , 0.49675406, 0.66192987,\n",
       "        0.81432862],\n",
       "       [0.02972227, 0.17379345, 0.3486173 , 0.51713414, 0.67735831,\n",
       "        0.82691851],\n",
       "       [0.20576812, 0.10607988, 0.28297865, 0.47536366, 0.64603187,\n",
       "        0.80145937],\n",
       "       [0.37860303, 0.3350413 , 0.14464721, 0.3535122 , 0.56245661,\n",
       "        0.73575633],\n",
       "       [0.54552996, 0.51625126, 0.41855167, 0.17114851, 0.40260992,\n",
       "        0.62215327],\n",
       "       [0.70394409, 0.68150547, 0.61083536, 0.47668244, 0.18931345,\n",
       "        0.4349027 ],\n",
       "       [0.8513734 , 0.83291601, 0.7761567 , 0.67566673, 0.51491647,\n",
       "        0.20039717],\n",
       "       [0.98551731, 0.96961683, 0.92131804, 0.83841114, 0.71522491,\n",
       "        0.53531928],\n",
       "       [1.10428256, 1.09011577, 1.04738843, 0.97525834, 0.87163196,\n",
       "        0.73127445],\n",
       "       [1.20581584, 1.19285551, 1.15393865, 1.08888967, 0.99714299,\n",
       "        0.87710558],\n",
       "       [1.28853277, 1.2764126 , 1.24012067, 1.17983286, 1.09572762,\n",
       "        0.98774444],\n",
       "       [1.35114258, 1.339589  , 1.30505511, 1.24790831, 1.16871236,\n",
       "        1.06813325],\n",
       "       [1.39266826, 1.38146202, 1.34800128, 1.29275433, 1.21648148,\n",
       "        1.12019965],\n",
       "       [1.41246182, 1.40141387, 1.36844107, 1.31405375, 1.2390927 ,\n",
       "        1.14471427]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 11 Numpy的where()函数和统计函数 \n",
    "\n",
    "## 11.1 where()函数\n",
    "\n",
    "where()函数的语法是 where(condition, [x, y])。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.arange(5, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "c = np.array([True, False, True, False, True])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False,  True, False,  True])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = np.empty(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6.95332845e-310, 2.12199579e-314, 3.16202013e-322, 0.00000000e+000,\n",
       "       0.00000000e+000])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 6, 2, 8, 4])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用where条件(bool元素为True时，选择a, 否则选择b)\n",
    "np.where(c, a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[2, -1], [-1, 2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2, -1],\n",
       "       [-1,  2]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0],\n",
       "       [0, 1]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(a > 0, 1, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 11.2 统计函数 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(9).reshape(3, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "36"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9, 12, 15])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum(axis = 0)  # 纵轴"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.0"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算数组元素的平均值\n",
    "a.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3., 4., 5.])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.mean(0)  # 纵轴"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0, array([0, 1, 2]))"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.min(), a.min(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8, array([6, 7, 8]))"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.max(), a.max(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2.581988897471611, 6.666666666666667)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算数组元素的标准差和方差\n",
    "a.std(), a.var()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0, 8)"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# argmin、 argmax计算最小、最大元素的索引。\n",
    "a.argmin(), a.argmax()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2],\n",
       "       [ 3,  5,  7],\n",
       "       [ 9, 12, 15]])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算元素的累加和\n",
    "a.cumsum(axis = 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 12 输入与输出\n",
    "\n",
    "## 12.1 二进制文件 \n",
    "\n",
    "`load()`, `save()`, `savez()`, `savez_compressed()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(16).reshape(4, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.save(\"./temp_array\", a)  #二进制文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.load(\"./temp_array.npy\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 12.2 文本文件\n",
    "\n",
    "`savetxt`、`loadtxt()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(16).reshape(4,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt(\"out.txt\", a, delimiter=\",\", fmt=\"%d\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  1.,  2.,  3.],\n",
       "       [ 4.,  5.,  6.,  7.],\n",
       "       [ 8.,  9., 10., 11.],\n",
       "       [12., 13., 14., 15.]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.loadtxt(\"out.txt\", delimiter=\",\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
